<?php

// @formatter:off
/**
 * @file WCParserBehaviorACTIONCALL.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: WCParserBehaviorACTIONCALL.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/mvc/WCParserBehaviorACTIONCALL.php $
 */
// @formatter:on

/**
 * @class WCParserBehaviorACTIONCALL This is a specific behavior that returns a variables
 * value. When it's used it looks like:
 * @code
 * <:ACTIONCALL:/>name<::/>
 * @endcode
 */
class WCParserBehaviorACTIONCALL extends WCParserBehavior {
	/**
	 * This method perform a check of the object, sets the current status
	 * (variable $this->_ok) and return it.
	 *
	 * @return boolean Returns the current status after a check.
	 */
	public function check() {
		//
		// A ACTIONCALL behavior must belong to a parser node.
		if($this->_node) {
			//
			// Loading node's children.
			$children = $this->_node->children();
			//
			// A ACTIONCALL behavior requires at least one child
			// belonging to its parser node.
//			if(!isset($children[0])) {
//				//
//				// Its parser node has no children. Error message
//				// is set and the internal status becomes false.
//				$this->_errorMsg = "node needs at least one child";
//				$this->_ok = false;
//			} elseif(!is_string($children[0])) {
//				//
//				// The first child of node is not a string and it
//				// cannot be considered as a name. Error message
//				// is set and the internal status becomes false.
//				$this->_errorMsg = "node's first child is not an url";
//				$this->_ok = false;
//			} else {
			//
			// At this point, everything checks out.
			$this->_ok = true;
			$this->_errorMsg = "Ok";
//			}
		} else {
			//
			// Behavior's node is not present. Error message is set
			// and the internal status becomes false.
			$this->_errorMsg = "ACTIONCALL node is not set";
			$this->_ok = false;
		}
		//
		// Returning current object status.
		return $this->isOk();
	}
	/**
	 * This method actually performs the function represented based on its
	 * node's data.
	 *
	 * @return string Returns the value of a variable using node's contents as
	 * a name. Otherwise, returns false.
	 */
	public function get() {
		//
		// Setting the default value to be returned.
		$out = false;
		//
		// ACTIONCALL behavior will only be evaluated when this behavior has no
		// issues.
		if($this->isOk()) {
			$out = "";
			//
			// Loading parameters.
			$params = $this->_node->params();
			//
			// Creating a shortcut to the parser.
			$parser = $this->_node->parser();
			//
			// Loading node's children.
			$children = $this->_node->children();
			//
			// 
			$content = $this->_node->parseChildren();
			//
			// 
			$viewManager = WCViewManager::I();
			if(isset($params["vaction"])) {
				$name = $parser->{$params["vaction"]};
			} else {
				$name = $params["action"];
			}
			$action = $viewManager->loadActionController($name);
			$out = $viewManager->buildOutput($action);

			//
			// When there's a parameter 'padding' set,
			// every line must be indented with as
			// many TAB characters as the number given
			// by the parameter 'padding'.
			if(isset($params["padding"])) {
				//
				// General spacer.
				$padding = str_repeat("\t", $params["padding"]);
				//
				// When the parameter 'nfl' is
				// given, the first line is not
				// indented.
				$out = (isset($params["nfl"]) ? "" : $padding).str_replace("\n", "\n".$padding, $out);
			}
		}
		//
		// Returning evaluation results.
		return $out;
	}
	//
	// Protected methods.
}
